package org.sanmao.wyj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.sanmao.wyj.controller.utils.R;
import org.sanmao.wyj.controller.utils.Result;
import org.sanmao.wyj.controller.utils.ResultCode;
import org.sanmao.wyj.domain.*;
import org.sanmao.wyj.service.MessageService;
import org.sanmao.wyj.service.OrderService;
import org.sanmao.wyj.service.WorkerService;
import org.sanmao.wyj.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("workers")
@RestController
public class WorkerController {

    @Autowired
    WorkerService workerService;
    @Autowired
    OrderService orderService;
    @Autowired
    MessageService messageService;
//worker登录拿token
    @PostMapping("/login")
    public Result login(@RequestBody Worker worker){
        String token = workerService.login(worker);
        if(token != null){
            return  Result.success(token);
        }
       else return Result.successButNoObject();
    }

//    注册账号
    @PostMapping("/register")
    public Result saveUser(@RequestBody Worker worker){
        if(workerService.saveWorker(worker)){
            return Result.success();
        }
        else return Result.failure(-1,"账号已存在！");
    }

    //    找回密码
    @PutMapping("/resetpassword")
    public Result findPassword(@RequestParam String username,@RequestParam String idcard,@RequestParam String password){
        if(workerService.findPassword(username,idcard,password)){
            return Result.success(true);
        }
        else return Result.failure(ResultCode.USER_NOT_EXIST);
    }

//    个人信息
    @GetMapping()
    public Result findMe(HttpServletRequest request){
        return Result.success(workerService.findMe(request));
    }
    
    //编辑个人信息
    @PutMapping
    public Result updateWorker(@RequestBody Worker worker,HttpServletRequest request){
        if(workerService.updateWorker(worker,request)){
            return Result.success();
        }
        else return Result.failure(-1,"已经有该跑腿员了！");
    }

    /**
     * 按type类型查询订单 分页
     * @param type 类型
     * @param currentPage 当前页
     * @param pageSize 数据条数
     * @return
     */
    @GetMapping(value = "/orders/{currentPage}/{pageSize}",params = {"type"})
    public Result findOrderByType(@RequestParam(value = "type") int type, @PathVariable int currentPage, @PathVariable int pageSize){
        return Result.success(orderService.findOrderByType(type,currentPage,pageSize));
    }

    //  按内容模糊查询订单
    @GetMapping(value = "/orders/{currentPage}/{pageSize}",params = {"type","con"})
    public Result fuzzyFind(@RequestParam(value = "con") String con,@RequestParam(value = "type")int type,@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Order> page = orderService.fuzzyFindOrderByConAndType(con, type, currentPage, pageSize);
        return Result.success(page);
    }

//    按state查找订单
    @GetMapping(value = "/orders/{currentPage}/{pageSize}",params = {"state"})
    public Result findOrderByState(@RequestParam(value = "state") int state,@PathVariable int currentPage, @PathVariable int pageSize,HttpServletRequest request){
        return Result.success(orderService.workerFindOrderByState(state,currentPage,pageSize,request));
    }

//    订单详情
    @GetMapping("/orders/{oid}")
    public Result findFullOrderById(@PathVariable int oid){
        return Result.success(orderService.findFullOrderByOid(oid));
    }



    //    接订单
    @PutMapping("/orders/take/{oid}")
    public Result updateOrder_Take(HttpServletRequest request,@PathVariable int oid){
       return Result.success(orderService.takeOrder(request,oid));
    }

//    完成订单
    @PutMapping("/orders/success/{oid}")
    public Result updateOrder_Over(@PathVariable int oid,HttpServletRequest request){
        int i = orderService.overOrder(oid, request);
        if(i > 0){
            return Result.success(i);
        }
       else return Result.failure(ResultCode.SPECIFIED_QUESTIONED_USER_NOT_EXIST);
    }

//   根据订单查询user的电话
    @GetMapping("/findUserPhonenum/{oid}")
    public Result findUserPhonenum(@PathVariable int oid){
        return Result.success(orderService.findUserPhonenumByOid(oid));
    }

    //查询留言
    @GetMapping("/messages/{currentPage}/{pageSize}")
    public Result findMsg(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<Message> page = new Page(currentPage,pageSize);
        return Result.success(messageService.page(page));
    }

    //留言
    @PostMapping("/messages")
    public Result addMsg(@RequestBody Message msg, HttpServletRequest request){
        return Result.success(messageService.workerSaveMsg(msg,request));
    }

    //删除留言
    @DeleteMapping("/messages/{mid}")
    public Result delMsgById(@PathVariable int mid,HttpServletRequest request){
        if(messageService.workerDelMsgById(mid,request)){
            return Result.success(true);
        }
        else return Result.failure(-1,"不能删除非自己的留言！");
    }

    //提现
    @PutMapping("/cashout")
    public Result cashOut(@RequestBody Cashout cash, HttpServletRequest request){
        if(workerService.cashOut(cash,request)){
            return Result.success(1,"提交提现成功！");
        }
        else return Result.failure(-1,"提交失败，请联系管理员！");
    }

}
